package zheezes.eisp.collect.model.impl.gbw;

import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;

import zheezes.eisp.collect.model.ConResMeterDailyFrozenCollectTaskHandler;
import zheezes.eisp.data.orm.Concentrator;
import zheezes.eisp.data.orm.ResMeter;
import zheezes.eisp.data.orm.ResMeterDailyFrozenData;
import zheezes.protocol.Frame;
import zheezes.protocol.impl.gbw.DataUnit;
import zheezes.protocol.impl.gbw.GbwConAddr;
import zheezes.protocol.impl.gbw.GbwFrame;
import zheezes.protocol.impl.gbw.afn8d.Afn8DDownF1S0DataUnit;
import zheezes.protocol.impl.gbw.afn8d.Afn8DUpF1DataUnit;
import zheezes.protocol.impl.gbw.bean.DailyFrozenData;
import zheezes.protocol.impl.gbw.bean.DailyFrozenDataParam;
import zheezes.protocol.impl.gbw.format.DataForm20;
import zheezes.socket.Transput;


public class GbwConResMeterDailyFrozenCollectTaskHandler extends
		ConResMeterDailyFrozenCollectTaskHandler {
	private static Logger logger = Logger
			.getLogger(GbwConResMeterDailyFrozenCollectTaskHandler.class);

	private int scnt = 0;

	public void run() {
		List<ResMeter> list = load();
		Calendar cal = Calendar.getInstance();
		cal.setTime(task.getDataDate());
		Concentrator con = dataAccessor.getConById(task.getConId());

		for (int i = 0; i < list.size() && !terminated; i++) {
			GbwFrame frame = new GbwFrame();
			frame.setDirection(GbwFrame.DIRECTION_DOWN);
			frame.setPrm(GbwFrame.PRM_STARTUP);
			frame.setConcentrator(new GbwConAddr(con.getLogicAddr()));
			frame.setAfn((byte) 0x8D);
			ResMeter meter = list.get(i);
			Afn8DDownF1S0DataUnit du = new Afn8DDownF1S0DataUnit();
			DailyFrozenDataParam param = new DailyFrozenDataParam();
			param.setSeq(meter.getSeq().shortValue());
			param.setDays((byte) 1);
			param.setSdate(new DataForm20(cal));
			du.addParam(param);
			frame.addDataUnit(du);

			// mailbox.clear();
			Transput transput;
			if ((transput = postOffice.send(frame)) != null) {
				Frame[] recv = null;
				if (transput.await() == Transput.SUCCESS	&& (recv = transput.getRecv()) != null) {
					// Object message = null;
					for (Frame message : recv) {
						// while((message = mailbox.get()) != null) {
						task.setCommSuccNum(task.getCommSuccNum() + 1);
						save((GbwFrame) message);
					}
				}
			}
		}
		task.setRunTimes(task.getRunTimes() + 1);
		refreshTaskStat();
		if (scnt > 0) {
			checkConResMeterDailyUse(con, cal);
		}
	}

	private void save(GbwFrame frame) {
		// int 0x8D == 141, byte 0x8D = -115
		if (frame.getAfn() == (byte) 0x8D) {
			for (DataUnit du : frame.getDataUnits()) {
				if (du.hasData()) {
					if (du instanceof Afn8DUpF1DataUnit) {
						Afn8DUpF1DataUnit unit = (Afn8DUpF1DataUnit) du;
						DailyFrozenData[] datas = unit.getDailyFrozenDatas();
						for (DailyFrozenData data : datas) {
							if (data.getValue().getValue() != null) {
								ResMeter rm;
								ResMeterDailyFrozenData dd = new ResMeterDailyFrozenData();
								if ((rm = findBySeq(data.getSeq())) != null) {
									dd.setMpid(rm.getMpId());
									dd.setDate(data.getValue().getCal().getTime());
									dd.setPp(data.getValue().getValue());
									if (dataAccessor.saveResMeterDailyFrozenData(dd) == 0) {
										scnt++;
									} else {
										logger.info("save resisdent failure");
									}
								} // if
							} // if
						} // for
					} // if
				} // if
			} // for
		} // if
	} // save()
}
